package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.SysAsset;
import com.ruoyi.system.domain.vo.SysAssetTransferVo;
import com.ruoyi.system.mapper.SysAssetMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysAssetTransferMapper;
import com.ruoyi.system.domain.SysAssetTransfer;
import com.ruoyi.system.service.ISysAssetTransferService;
import org.springframework.util.CollectionUtils;

/**
 * 资产调拨Service业务层处理
 * 
 * @author sxy
 * @date 2025-03-26
 */
@Service
public class SysAssetTransferServiceImpl implements ISysAssetTransferService 
{
    @Autowired
    private SysAssetTransferMapper sysAssetTransferMapper;
    @Autowired
    private SysAssetMapper sysAssetMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询资产调拨
     * 
     * @param transferId 资产调拨主键
     * @return 资产调拨
     */
    @Override
    public SysAssetTransferVo selectSysAssetTransferByTransferId(Long transferId)
    {
        SysAssetTransfer transfer = sysAssetTransferMapper.selectSysAssetTransferByTransferId(transferId);
        SysAssetTransferVo transferVo = new SysAssetTransferVo();
        BeanUtils.copyProperties(transfer, transferVo);
        //查询资产名称
        if(transfer.getAssetId() != null){
            transferVo.setAssetName(sysAssetMapper.selectSysAssetByAssetId(transfer.getAssetId()).getAssetName());
            transferVo.setAssetNo(sysAssetMapper.selectSysAssetByAssetId(transfer.getAssetId()).getAssetNo());
        }
        //查询原部门名称
        if(transfer.getOldDeptId() != null){
            transferVo.setOldDept(sysDeptMapper.selectDeptById(transfer.getOldDeptId()).getDeptName());
        }
        //查询新部门名称
        if(transfer.getNewDeptId() != null){
            transferVo.setNewDept(sysDeptMapper.selectDeptById(transfer.getNewDeptId()).getDeptName());
        }
        //查询原使用人名称
        if(transfer.getOldUserId() != null){
            transferVo.setOldUser(sysUserMapper.selectUserById(transfer.getOldUserId()).getUserName());
        }
        //查询新使用人名称
        if(transfer.getNewUserId() != null){
            transferVo.setNewUser(sysUserMapper.selectUserById(transfer.getNewUserId()).getUserName());
        }
        //查询操作人名称
        if(transfer.getOperatorId() != null){
            transferVo.setOperator(sysUserMapper.selectUserById(transfer.getOperatorId()).getUserName());
        }
        return transferVo;
    }

    /**
     * 查询资产调拨列表
     * 
     * @param sysAssetTransfer 资产调拨
     * @return 资产调拨
     */
    @Override
    public List<SysAssetTransferVo> selectSysAssetTransferList(SysAssetTransfer sysAssetTransfer)
    {
        List<SysAssetTransfer> sysAssetTransfers = sysAssetTransferMapper.selectSysAssetTransferList(sysAssetTransfer);
        List<SysAssetTransferVo> rsp = new ArrayList<>();
        if(!CollectionUtils.isEmpty(sysAssetTransfers)){
            for (SysAssetTransfer transfer : sysAssetTransfers) {
                SysAssetTransferVo transferVo = new SysAssetTransferVo();
                BeanUtils.copyProperties(transfer, transferVo);
                //查询资产名称
                if(transfer.getAssetId() != null){
                    transferVo.setAssetName(sysAssetMapper.selectSysAssetByAssetId(transfer.getAssetId()).getAssetName());
                    transferVo.setAssetNo(sysAssetMapper.selectSysAssetByAssetId(transfer.getAssetId()).getAssetNo());
                }
                //查询原部门名称
                if(transfer.getOldDeptId() != null){
                    transferVo.setOldDept(sysDeptMapper.selectDeptById(transfer.getOldDeptId()).getDeptName());
                }
                //查询新部门名称
                if(transfer.getNewDeptId() != null){
                    transferVo.setNewDept(sysDeptMapper.selectDeptById(transfer.getNewDeptId()).getDeptName());
                }
                //查询原使用人名称
                if(transfer.getOldUserId() != null){
                    transferVo.setOldUser(sysUserMapper.selectUserById(transfer.getOldUserId()).getUserName());
                }
                //查询新使用人名称
                if(transfer.getNewUserId() != null){
                    transferVo.setNewUser(sysUserMapper.selectUserById(transfer.getNewUserId()).getUserName());
                }
                //查询操作人名称
                if(transfer.getOperatorId() != null){
                    transferVo.setOperator(sysUserMapper.selectUserById(transfer.getOperatorId()).getUserName());
                }
                rsp.add(transferVo);
            }
        }
        return rsp;
    }

    /**
     * 新增资产调拨
     * 
     * @param sysAssetTransfer 资产调拨
     * @return 结果
     */
    @Override
    public int insertSysAssetTransfer(SysAssetTransfer sysAssetTransfer)
    {
        int i = sysAssetTransferMapper.insertSysAssetTransfer(sysAssetTransfer);
        if(i > 0){
            //新增成功后，修改资产表的位置，使用部门，使用人等相关信息
            SysAsset asset = new SysAsset();
            asset.setAssetId(sysAssetTransfer.getAssetId());
            asset.setLocation(sysAssetTransfer.getNewLocation());
            asset.setDeptId(sysAssetTransfer.getNewDeptId());
            asset.setUserId(sysAssetTransfer.getNewUserId());
            sysAssetMapper.updateSysAsset(asset);
        }
        return i;
    }

    /**
     * 修改资产调拨
     * 
     * @param sysAssetTransfer 资产调拨
     * @return 结果
     */
    @Override
    public int updateSysAssetTransfer(SysAssetTransfer sysAssetTransfer)
    {
        int i = sysAssetTransferMapper.updateSysAssetTransfer(sysAssetTransfer);
        if(i > 0){
            //修改成功后，修改资产表的位置，使用部门，使用人等相关信息
            SysAsset asset = new SysAsset();
            asset.setAssetId(sysAssetTransfer.getAssetId());
            asset.setLocation(sysAssetTransfer.getNewLocation());
            asset.setDeptId(sysAssetTransfer.getNewDeptId());
            asset.setUserId(sysAssetTransfer.getNewUserId());
            sysAssetMapper.updateSysAsset(asset);
        }
        return i;
    }

    /**
     * 批量删除资产调拨
     * 
     * @param transferIds 需要删除的资产调拨主键
     * @return 结果
     */
    @Override
    public int deleteSysAssetTransferByTransferIds(Long[] transferIds)
    {
        return sysAssetTransferMapper.deleteSysAssetTransferByTransferIds(transferIds);
    }

    /**
     * 删除资产调拨信息
     * 
     * @param transferId 资产调拨主键
     * @return 结果
     */
    @Override
    public int deleteSysAssetTransferByTransferId(Long transferId)
    {
        return sysAssetTransferMapper.deleteSysAssetTransferByTransferId(transferId);
    }
}
